package com.ywgt;

public class Stack {
    public static void main(String[] args) {
        StackDemo stackDemo=new StackDemo(5);
        stackDemo.push(10);
        stackDemo.push(20);
        stackDemo.push(30);
        stackDemo.push(40);
        System.out.println(stackDemo.pop());
        System.out.println(stackDemo.pop());
        stackDemo.list();
        System.out.println(stackDemo.pop());
        System.out.println(stackDemo.pop());
    }
}

class StackDemo{
    int top;
    int [] val;
    int foot;
    int maxSize;
    public StackDemo( int val) {
        this.val = new int[val];
        this.top=-1;
        this.foot=-1;
        this.maxSize=val;
    }


    public boolean isFull(){
        return val.length-1==top;
    }

    public boolean isEmpty(){
        return top==-1;
    }
    public void push(int n){
        if(isFull()){
            System.out.println("栈满");
            return;
        }
        if(top==-1){

            foot++;
        }
        top++;
        val[top]=n;
    }

    public int pop(){
        if(isEmpty()){
            System.out.println("栈已空");
            return -1;
        }
        int res=val[top];
        top--;
        if(top==-1){
            foot--;
        }
        return res;
    }

    public void list(){
        if(isEmpty()){
            System.out.println("栈空");
            return;
        }
        for (int i = top; i >=0; i--) {
            System.out.printf("stack[%d] %d",i,val[i]);
            System.out.println();
        }
    }
}
